package pl.un4giv3n.mightvsmagic.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

@NamedQueries({
	@NamedQuery(name="find.clan.by.name", query="select c from Clan c where c.name=:name"),
	@NamedQuery(name="find.all.clans", query="select c from Clan c")
})
@Entity
public class Clan {

	private Long id;
	private Guardian guardian;
	private int altarLevel = 1;
	private List<User> users;
	private int barracksLevel = 1;
	private int towerLevel = 1;
	private String name;
	private long gold;
	
	@GeneratedValue
	@Id
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	@ManyToOne(targetEntity=Guardian.class)
	public Guardian getGuardian() {
		return guardian;
	}
	public void setGuardian(Guardian guardian) {
		this.guardian = guardian;
	}
	public int getAltarLevel() {
		return altarLevel;
	}
	void setAltarLevel(int altarLevel) {
		this.altarLevel = altarLevel;
	}
	@OneToMany
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public void setName(String clanName) {
		this.name = clanName;
	}
	public String getName() {
		return name;
	}
	
	@Transient
	public void addUser(User user){
		if(users == null){
			users = new ArrayList<User>();
		}
		if(!users.contains(user)){
			users.add(user);
		}
	}
	public int getBarracksLevel() {
		return barracksLevel;
	}
	void setBarracksLevel(int barracksLevel) {
		this.barracksLevel = barracksLevel;
	}
	public int getTowerLevel() {
		return towerLevel;
	}
	void setTowerLevel(int towerLevel) {
		this.towerLevel = towerLevel;
	}
	public long getGold() {
		return gold;
	}
	void setGold(long gold) {
		this.gold = gold;
	}
	@Transient
	public void addGold(long gold){
		this.gold +=gold;
	}
	@Transient
	public void removeGold(long gold){
		this.gold -=gold;
	}
	@Transient
	public void improveTowerLevel(int towerLevel) {
		this.towerLevel += towerLevel;
	}
	@Transient
	public void improveBarracksLevel(int towerLevel) {
		this.towerLevel += towerLevel;
	}
	@Transient
	public void improveAltarLevel(int towerLevel) {
		this.towerLevel += towerLevel;
	}
}
